<template>
	<view>

	</view>
</template>

<script>
	let that, callBackFn
	// #ifdef APP-PLUS
	import permision from "@/common/permission.js"
	// #endif
	import {
		mapActions,
		mapGetters,
	} from 'vuex'
	export default {
		computed: {
			...mapGetters(['api', 'userInfo', 'userToken'])
		},
		data() {
			return {
				loading: false,
				form: {
					username: '',
					password: '',
				},
				remember: [], //是否记住密码
				isAgree: false, // 是否同意用户协议
			}
		},
		onLoad() {
			that = this
		},
		methods: {
			// 扫码
			async customBack(callBack) {
				callBackFn = callBack
				// #ifdef APP-PLUS
				let status = await this.checkPermission();
				if (status !== 1) {
					return;
				}
				setTimeout(() => {
					this.scan()
				}, 1000)
				// #endif 
				// #ifndef APP-PLUS
				this.scan()
				// #endif 
			},
			async checkPermission(code) {
				let status = permision.isIOS ? await permision.requestIOS('camera') :
					await permision.requestAndroid('android.permission.CAMERA');

				if (status === null || status === 1) {
					status = 1;
				} else {
					uni.showModal({
						content: "需要相机权限",
						confirmText: "设置",
						success: function(res) {
							if (res.confirm) {
								permision.gotoAppSetting();
							}
						}
					})
				}
				return status;
			},
			//扫码
			scan() {
				uni.scanCode({
					success: (res) => {
						try {
							let url = decodeURIComponent(decodeURIComponent(res.result))
							let data = JSON.parse(url)
							if (data && data.cmd == 'yunxi') {
								// 判断二维码过期时间
								let time = new Date().getTime()
								if (data.expireTime < time) {
									uni.showToast({
										title: '二维码已过期',
										icon: 'none'
									});
									return
								}
								uni.$u.http.setConfig((config) => {
									if (data.host) {
										uni.setStorageSync('baseUrl', data.host)
										config.baseURL = data.host; 
									}
									if (data.tenantId) {
										config.header.tenantId= data.tenantId
									}
								    return config
								})
								
								uni.setStorageSync('scanData', data)
								callBackFn(data)
								uni.showToast({
									title: '扫码成功',
									icon: 'success'
								});

							} else {
								uni.showToast({
									title: '无效二维码',
									icon: 'none'
								});
							}
						} catch (e) {
							//TODO handle the exception
							uni.showToast({
								title: '无效二维码',
								icon: 'none'
							});
						}
					}
				});
			},

		}
	}
</script>

<style scoped lang="scss">

</style>